package com.lzw.mall.order.controller;


import com.lzw.mall.common.service.ICartService;
import com.lzw.mall.common.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhenwei
 * @since 2023-04-04
 */
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    @GetMapping("/index")
    public String index() {
        return "cart";
    }

    @GetMapping("/list")
    @ResponseBody
    public List<CartVO> list() {
        return cartService.selectList();
    }

    @PostMapping("/del")
    @ResponseBody
    public void del(Integer cartid){
        cartService.doDel(cartid);
    }
    @PostMapping("/updataAmount")
    @ResponseBody
    public void updataAmount(Integer cartid,Integer amount){
        cartService.updataAmount(cartid,amount);
    }

    @PostMapping("/confirm")
    public String confirm(String ids, Model model){

        model.addAttribute("adds",null);
        model.addAttribute("ids",ids);
        List<CartVO> list=cartService.selectListByIds(ids);
        BigDecimal sum=new BigDecimal(0);
        for (CartVO cart:list) {
            sum=sum.add(cart.getPrice().multiply(new BigDecimal(cart.getAmount())));
        }

        model.addAttribute("sum",sum);
        model.addAttribute("carts",list);
        return "confirm";
    }

}
